package bob.project.object;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

import bob.project.ResourceManager;
import bob.project.slick.GameState;

public class Player extends LiveObject
{
	public int energy = 100;
	public int maxEnergy = 100;
	public int lives = 3;
	public boolean left = true;
	public boolean onGround = true;
	public boolean isMove = false;
	Animation move_left;
	Animation move_right;
	Animation jump_left;
	Animation jump_right;
	
	Animation weapon_hands;
	Animation weapon_shotgun;
	Animation weapon_smg;
	Image withoutWeapon;
	
	Image stay_left;
	Image stay_right;	
	float ySpeed = 0;
	
	public Player()
	{
		x = 200;
		y = 300;
		width = 24;
		height = 46;
		Image sheet = ResourceManager.getInstance().getImage("player");
		rect = new Rectangle(0,23,24,23);
		move_right  = new Animation();
		for(int i = 0;i< 5;i++)
			move_right.addFrame(sheet.getSubImage(i*24+i*12, 0, 24, 46), 100);
		
		move_left  = new Animation();
		for(int i = 0;i< 5;i++)
			move_left.addFrame(sheet.getSubImage(i*24+i*12, 0, 24, 46).getFlippedCopy(true, false), 100);
		
		jump_right  = new Animation();
		for(int i = 0;i< 5;i++)
			jump_right.addFrame(sheet.getSubImage(i*24+i*12, 46+46+18+18, 24, 46), 200);
		//jump_right.setLooping(false);
		jump_left  = new Animation();
		for(int i = 0;i< 5;i++)
			jump_left.addFrame(sheet.getSubImage(i*24+i*12, 46+46+18+18, 24, 46).getFlippedCopy(true, false), 200);
		//jump_left.setLooping(false);
		withoutWeapon = sheet.getSubImage(0, 46+46+18+18+46+46+18+18, 24, 46);
		
		weapon_hands = new Animation();
		
		stay_right = sheet.getSubImage(0, 46+18, 24, 46);
		stay_left = sheet.getSubImage(0, 46+18, 24, 46).getFlippedCopy(true, false);
		//for
		//
		
	}
	
	@Override
	public void die()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void update(int delta)
	{
		isMove = false;

		boolean[] wasd = GameState.instance.wasd;

		
		if (wasd[0] && onGround)
			jump();
		if (wasd[1])
		{
			left = true;
			if (tryMoveTo(x-1,y))
			{
				isMove = true;
				x--;
			}
		}
		else if (wasd[3])
		{
			left = false;
			if (tryMoveTo(x+1,y))
			{
				isMove = true;
				x++;
			}
		}
		if (wasd[2])
		{
			//
		}
		
		if (!onGround)
		{
			if (tryMoveTo(x,y+(ySpeed+1)/1000f*delta))
			{
				
				ySpeed+= 8f;
				if (ySpeed >= 256f)
					ySpeed = 256f;
				y+=ySpeed/1000f*delta;
			}
			else
			{
				ySpeed = 0;
				onGround = true;
			}
		}

		if (left)
		{
			if (onGround)
			{
				if (isMove)
					move_left.update(delta);
			}
			else
				jump_left.update(delta);
		}
		else
		{
			if (onGround)
			{
				if (isMove)
					move_right.update(delta);
			}
			else
				jump_right.update(delta);
		}
		
	}

	private void jump()
	{
		onGround = false;
		ySpeed = -256f;
		if (left)
			jump_left.start();
		else
			jump_right.start();
		
	}

	@Override
	public void draw(Graphics g)
	{
		if (left)
		{
			if (onGround)
			{
				if (isMove)
					move_left.draw(x, y);
				else
					stay_left.draw(x, y);
			}
			else
				jump_left.draw(x, y);
		}
		else
		{
			if (onGround)
			{
				if (isMove)
					move_right.draw(x, y);
				else
					stay_right.draw(x, y);
			}
			else
				jump_right.draw(x, y);
		}
		//g.draw(rect);
		//g.drawImage(stay,100,100);
		//g.drawImage(withoutWeapon,333,100);
		
		//move_left.draw(111, 111);
		//jump_left.draw(222, 111);
	}

}
